package com.ruoyi.yljf.util;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.yljf.domain.vo.CommonVO;
import com.ruoyi.yljf.domain.vo.DrugResVO;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

import static cn.hutool.extra.pinyin.PinyinUtil.isChinese;


@Component
public class CommonUtils {

    @Autowired
    TokenService tokenService;

    public Map<Character, List<CommonVO>> Listsort(List<CommonVO> list){
        Collator collator = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, (u1, u2) -> collator.compare(u1.getName(), u2.getName()));
        for (CommonVO vo : list) {
            System.out.println(vo);
        }
        Map<Character, List<CommonVO>> groupedUsers = list.stream()
                .collect(Collectors.groupingBy(item -> getFirstLetter(item.getName())));
        List<Character> keyList = new ArrayList<>(groupedUsers.keySet());
        Collections.sort(keyList);

        Map<Character, List<CommonVO>> resMap = new LinkedHashMap<>(keyList.size());
        for (Character key : keyList) {
            System.out.printf(String.valueOf(key));
            resMap.put(key, groupedUsers.get(key));
        }
        return resMap;
    }



    /**
     * 获取中文名字的拼音首字母
     */
    private static Character getFirstLetter(String name) {
        if (name == null || name.isEmpty()) {
            return null;
        }
        char firstChar = name.charAt(0);
        if (isChinese(firstChar)) {
            try {
                // 如果是中文，转换为拼音并获取首字母
                HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
                format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
                format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(firstChar, format);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    return pinyinArray[0].charAt(0);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }
        // 如果不是中文，直接返回首字母
        return Character.toUpperCase(firstChar);
    }

    public LinkedList<DrugResVO> ListsortV2(List<CommonVO> list) {
        Collator collator = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, (u1, u2) -> collator.compare(u1.getName(), u2.getName()));
        Map<Character, List<CommonVO>> groupedUsers = list.stream()
                .collect(Collectors.groupingBy(item -> getFirstLetter(item.getName())));
        List<Character> keyList = new ArrayList<>(groupedUsers.keySet());
        Collections.sort(keyList);
        LinkedList<DrugResVO> drugResVOS = new LinkedList<>();
        for (Character key : keyList) {
            System.out.printf(String.valueOf(key));
            DrugResVO drugResVO = new DrugResVO();
            drugResVO.setKey(key.toString());
//                drugResVO.setResList();
            drugResVO.setValue( groupedUsers.get(key));
            drugResVOS.add(drugResVO);
        }
        return drugResVOS;
    }


    /** 判断用户是否登录 */

    public boolean userIfLogin(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (ObjectUtils.isNotEmpty(loginUser)) {
            return true;
        }
        return false;
    }


}
